﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M014_BILLSCAN : FormBase
    {
        public M014_BILLSCAN()
        {
            InitializeComponent();
        }

        #region 变量定义

        BLL.BLL_M014BILLSCAN bll_M014BILLSCAN = new XiaoGang.Soft.XGWMS.BLL.BLL_M014BILLSCAN();
        string strGroupTitle = "检索结果@@@   提示：双击各货单可查看该货单包含的详细商品信息";
        private const string dataName = "流水";
        
        #endregion

        #region 窗口初始化


        /// <summary>	
        /// <p><b>方法名称</b>SetTabIndex</p>	
        /// <p><b>概要説明	:</b>画面フォーカスの遷移順の設定</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// </summary>
        private void SetTabIndex()
        {
            int intIndex = 0;           
            txtBILL_ID.TabIndex = intIndex; intIndex++;
            grpIN_OUT.TabIndex = intIndex; intIndex++;
            txtBUSINESS_TIME_START.TabIndex = intIndex; intIndex++;
            txtBUSINESS_TIME_END.TabIndex = intIndex; intIndex++;
            btnSearch.TabIndex = intIndex; intIndex++;
        }

        /// <summary>	
        /// <p><b>方法名称</b>ClearControl</p>	
        /// <p><b>概要説明	:</b>画面上の全項目をオールクリアする</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// </summary>
        private void ClearControl()
        {
            txtBILL_ID.Text = "";  
            chkInPut.Checked = false;
            chkOutPut.Checked = false;
            txtBUSINESS_TIME_START.Text = DateTime.Now.ToString("yyyyMMdd").Insert(4, "/").Insert(7, "/");
            txtBUSINESS_TIME_END.Text = DateTime.Now.ToString("yyyyMMdd").Insert(4, "/").Insert(7, "/");
            grpSearchResult.Text = strGroupTitle.Replace("@@@", "");
        }

        /// <summary>	
        /// <p><b>方法名称</b>SetEnable</p>	
        /// <p><b>概要説明  :</b>画面モードのセト</p>
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        private void SetEnable()
        {
            grpBillSearch.Enabled = true;
            txtBILL_ID.Enabled = true;
            txtBUSINESS_TIME_START.Enabled = true;
            dtpBUSINESS_TIME_START.Enabled = true;
            txtBUSINESS_TIME_END.Enabled = true;
            dtpBUSINESS_TIME_END.Enabled = true;
            grpIN_OUT.Enabled = true;
            chkInPut.Enabled = true;
            chkOutPut.Enabled = true;
            btnSearch.Enabled = true;

            grpSearchResult.Enabled = true;
            grdmentBillInfo.Enabled = true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>InitialGridView</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// </summary>
        private void InitialGridView()
        {
            grdmentBillInfo.ReadOnly = true;
            grdmentBillInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdmentBillInfo.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdmentBillInfo.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdmentBillInfo.ColumnHeadersHeight = 20;
            grdmentBillInfo.ColumnHeadersVisible = true;
            grdmentBillInfo.AllowUserToResizeColumns = true;
            grdmentBillInfo.ColumnHeadersDefaultCellStyle.Alignment =
                DataGridViewContentAlignment.MiddleCenter;

            grdmentBillInfo.Columns.Add("NO.", "NO.");
            grdmentBillInfo.Columns.Add("BILL_ID", "货单编号");
            grdmentBillInfo.Columns.Add("IN_OUT", "进出库区分");
            grdmentBillInfo.Columns.Add("BUSINESS_TIME", "业务时间");
            grdmentBillInfo.Columns.Add("MEMO", "备注");

            grdmentBillInfo.Columns["NO."].Width = 30;
            grdmentBillInfo.Columns["NO."].Frozen = true;
            grdmentBillInfo.Columns["NO."].DefaultCellStyle.Alignment =
                DataGridViewContentAlignment.MiddleCenter;
            grdmentBillInfo.Columns["BILL_ID"].Width = 130;
            grdmentBillInfo.Columns["BILL_ID"].Frozen = true;
            grdmentBillInfo.Columns["BILL_ID"].Resizable = DataGridViewTriState.False;
            grdmentBillInfo.Columns["IN_OUT"].Width = 100;            
            grdmentBillInfo.Columns["IN_OUT"].Resizable = DataGridViewTriState.False;
            grdmentBillInfo.Columns["BUSINESS_TIME"].Width = 150;
            grdmentBillInfo.Columns["MEMO"].Width = 353;           

            grdmentBillInfo.AllowUserToAddRows = false;
            grdmentBillInfo.AllowUserToDeleteRows = false;
            grdmentBillInfo.AllowUserToResizeRows = false;
            grdmentBillInfo.RowHeadersVisible = false;
            grdmentBillInfo.RowTemplate.Height = 20;
            grdmentBillInfo.RowTemplate.ReadOnly = true;
            grdmentBillInfo.RowTemplate.Resizable = DataGridViewTriState.False;
        }        

        /// <summary>	
        /// <p><b>方法名称</b>InitProcess</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// </summary>
        private void InitProcess()
        {
            SetTabIndex();
            SetEnable();
            InitialGridView();
            ClearControl();
            ShowGridData();
        }

        #endregion

        #region 方法

        /// <summary>	
        /// <p><b>方法名称</b>CheckProcess</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" param="strConstrolName">控件名称</param>	
        /// <returns type="Boolean">　True:成功   False:失敗 </returns>
        protected override bool CheckProcess(string strConstrolName)
        {
            switch (strConstrolName)
            {
                case "txtBILL_ID":
                    if (Method.CheckIsNumber(txtBILL_ID.Text.Trim(),
                        txtBILL_ID.MaxLength, true, true) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg,dataName);
                        txtBILL_ID.Focus();
                        return false;
                    }
                    break;
                case "txtBUSINESS_TIME_START":
                    if (CheckProcess(txtBUSINESS_TIME_START.Text.Trim(), enumCheckItems.DateYMD,
                        true, null) == false)
                    {
                        txtBUSINESS_TIME_START.Focus();
                        return false;
                    }
                    break;
                case "txtBUSINESS_TIME_END":
                    if (CheckProcess(txtBUSINESS_TIME_END.Text.Trim(), enumCheckItems.DateYMD,
                        true, null) == false)
                    {
                        txtBUSINESS_TIME_END.Focus();
                        return false;
                    }
                    if (CheckBusniessStartDateLessBusinessEndDate() == false)
                    {
                        return false;
                    }
                    break;
            }
            return true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>CheckProcess</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <returns type="Boolean">　True:成功   False:失敗 </returns>
        private bool CheckProcessLoop()
        {
            if (CheckProcess("txtBILL_ID") == false) return false;
            if (CheckProcess("txtBUSINESS_TIME_START") == false) return false;
            if (CheckProcess("txtBUSINESS_TIME_END") == false) return false;            
            return true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>ShowGrid</p>	
        /// <p><b>概要説明  :</b>検索結果をGridに表示する</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/08</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" param="model">库存信息表</param>	
        private void ShowGrid(MODEL.MOD_M010_BILL model)
        {
            ReturnValue rvReturnValue = bll_M014BILLSCAN.SearchProcess(model);
            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return;
            }
            if (0 == rvReturnValue.ResultDataSet.Tables[0].Rows.Count)
            {                
                grdmentBillInfo.Rows.Clear();
                grpSearchResult.Text = strGroupTitle.Replace("@@@", "");
                grdmentBillInfo.Columns["BUSINESS_TIME"].Width = 123;
                return;
            }
            foreach (DataRow row in rvReturnValue.ResultDataSet.Tables[0].Rows)
            {
                grdmentBillInfo.Rows.Add(
                    (grdmentBillInfo.Rows.Count + 1).ToString(),
                    row["BILL_ID"].ToString(),
                    row["IN_OUT"].ToString(),
                    DateFormat(row["BUSINESS_TIME"].ToString().Replace("/","")),
                    row["MEMO"].ToString());
            }
            grpSearchResult.Text = strGroupTitle.Replace("@@@","  共" + rvReturnValue.ResultDataSet.Tables[0].Rows.Count.ToString() + "条");
            if (grdmentBillInfo.Rows.Count > 21)
            {
                grdmentBillInfo.Columns["BUSINESS_TIME"].Width = 133;
                grdmentBillInfo.Columns["MEMO"].Width = 353; 
            }
            else
            {
                grdmentBillInfo.Columns["BUSINESS_TIME"].Width = 150;
                grdmentBillInfo.Columns["MEMO"].Width = 353; 
            }
        }

        /// <summary>	
        /// <p><b>方法名称</b>DateFormat</p>	
        /// <p><b>概要説明  :</b>date data format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/18</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="strValue">没有格式化的字符串</param> 
        /// <returns type="string">格式化后的字符串</returns>
        protected string DateFormat(string strValue)
        {
            if (strValue.Length < 8)
            {
                return strValue;
            }
            return strValue.Substring(0, 4) + "/" +
                strValue.Substring(4, 2) + "/" +
                strValue.Substring(6, 2);
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckProductDateLessOverProductDate</p>	
        /// <p><b>概要説明  :</b>生产日期小于过期日期的Check</p>
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/18</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <returns type = "bool">false:生产日期大于过期日期 true:生产日期小于过期日期</returns>
        protected bool CheckBusniessStartDateLessBusinessEndDate()
        {
            if (0 == txtBUSINESS_TIME_START.Text.Trim().Length ||
                0 == txtBUSINESS_TIME_END.Text.Trim().Length)
            {
                return true;
            }
            try
            {
                if (DateTime.Compare(
                    DateTime.Parse(DateFormat(txtBUSINESS_TIME_START.Text.Trim().Replace("/", "")) + " 00:00:00"),
                    DateTime.Parse(DateFormat(txtBUSINESS_TIME_END.Text.Trim().Replace("/", "")) + " 00:00:00")
                                    ) > 0
                   )
                {
                    Method.MessageShow("W11034",dataName);
                    txtBUSINESS_TIME_END.Focus();
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion 

        #region 事件

        private void M014_BILLSCAN_Load(object sender, EventArgs e)
        {
            InitProcess();
            txtBILL_ID.Focus();
        }

        private void grdmentBillInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (0 == grdmentBillInfo.Rows.Count || e.RowIndex == -1)
            {
                return;
            }
            string strBillID = grdmentBillInfo.CurrentRow.Cells["BILL_ID"].Value.ToString();
            M014_BILLSCAN_Detail frm_M014_BILLSCAN_Detail = new M014_BILLSCAN_Detail(strBillID);
            frm_M014_BILLSCAN_Detail.StartPosition = FormStartPosition.CenterScreen;
            frm_M014_BILLSCAN_Detail.ShowDialog();
        }

        protected override void AllTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }

        private void AllTextBox_KeyPrecess(object sender, KeyPressEventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim();
            switch (((TextBox)sender).Name)
            {
                case "txtBILL_ID":
                    FormBase.UKeyPress(e, InputType.Num);
                    break;
                case "txtBUSINESS_TIME":
                    FormBase.UKeyPress(e, InputType.Date);
                    break;
            }
        }

        private void AllTextBox_GotFocus(object sender, EventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim();
            switch (((TextBox)sender).Name)
            {
                case "txtBILL_ID":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    break;
                case "txtBUSINESS_TIME_START":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    ((TextBox)sender).Text = 
                        ((TextBox)sender).Text.Replace("/", "");
                    break;
                case "txtBUSINESS_TIME_END":
                    ((TextBox)sender).SelectAll();
                    ((TextBox)sender).ImeMode = ImeMode.Disable;
                    ((TextBox)sender).Text =
                        ((TextBox)sender).Text.Replace("/", "");
                    break;
            }
        }

        private void AllTextBox_LostFocus(object sender, EventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim().Replace("/", "");
            strValue = strValue.Replace(",", "");
            switch (((TextBox)sender).Name)
            {
                case "txtBUSINESS_TIME_START":
                    ((TextBox)sender).Text = DateFormat(strValue);
                    break;
                case "txtBUSINESS_TIME_END":
                    ((TextBox)sender).Text = DateFormat(strValue);
                    break;
            }
        }

        private void AllButton_Click(object sender, EventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }
            Cursor.Current = Cursors.WaitCursor;

            string strButtonName = ((Button)sender).Name;

            switch (strButtonName)
            {
                #region btnSearch
                case "btnSearch":
                    {
                        if (CheckProcessLoop() == false)
                        {
                            Cursor.Current = Cursors.Default;
                            return;
                        }
                        ShowGridData();
                    }
                    break;
                #endregion               
            }
            Cursor.Current = Cursors.Default;
        }

        private void dtpBUSINESS_TIME_START_CloseUp(object sender, EventArgs e)
        {
            txtBUSINESS_TIME_START.Text = 
                Method.TimeFormat( dtpBUSINESS_TIME_START.Value.ToString("yyyyMMdd"));
        }

        private void dtpBUSINESS_TIME_END_CloseUp(object sender, EventArgs e)
        {
            txtBUSINESS_TIME_END.Text =
                Method.TimeFormat(dtpBUSINESS_TIME_END.Value.ToString("yyyyMMdd"));
        }

        private void ShowGridData()
        {
            MODEL.MOD_M010_BILL model = new XiaoGang.Soft.XGWMS.MODEL.MOD_M010_BILL();

            model.BILL_ID = txtBILL_ID.Text.Trim();

            if ((chkInPut.Checked == true && chkOutPut.Checked == true)
                || (chkInPut.Checked == false && chkOutPut.Checked == false))
            {
                model.IN_OUT = "";
            }
            if (chkInPut.Checked == true && chkOutPut.Checked == false)
            {
                model.IN_OUT = "0";//IN
            }
            if (chkInPut.Checked == false && chkOutPut.Checked == true)
            {
                model.IN_OUT = "1";//OUT
            }
            model.BUSINESS_START_TIME = txtBUSINESS_TIME_START.Text.Trim().Replace("/", "");
            model.BUSINESS_END_TIME = txtBUSINESS_TIME_END.Text.Trim().Replace("/", "");
            grdmentBillInfo.Rows.Clear();
            ShowGrid(model);
        }

        #endregion
    }
}